<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->helper('file');
		$this->load->model('admin_model');
		$this->load->model('dieta_model');
		$this->load->helper('xml');
	}
	
	function index()
	{
		$data['title'] = 'NIS - Admin';
		$this->load->library('form_validation');
		if ($this->form_validation->run()) // validation ok
		{	
		   if ($_FILES['subor']['tmp_name'])
	       {
			  $this->_upload();
	        }
		}
		$this->load->view('templates/header', $data);
		$this->load->view('project/admin_view');
		$this->load->view('templates/footer');
		
		
	}
	
	function pridaj_pacienta() { redirect('project/register_pacienta'); }
	function pridaj_personal() { redirect('project/register_personalu'); }
	function pridaj_jedlo() { redirect('jedlo/pridaj'); }
	


	 
	function do_upload()
	{
		$config['upload_path'] = './application/uploads/';
		$config['allowed_types'] = 'xml';
		$config['max_size']	= '10000';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
			$data['title'] = 'titulka';
	
			$this->load->view('templates/header', $data);
			echo "neuspech<br/>";
			$this->load->view('templates/footer');
		}
		else
		{
			$this->admin_model->importfromxml($this->upload->data());
			
			$data['title'] = 'titulka';
		
			$this->load->view('templates/header', $data);			  
			$this->load->view('project/success', $data);
			$this->load->view('templates/footer');

		}
	}	
	
	function export()
	{

		$query = $this->db->select('Jedlo.ID, Jedlo.Nazov_jedla, Jedlo.Hmotnost, Jedlo.Vyzivova_hmota, Obsahuje.Nazov')->from('Jedlo')->join('Obsahuje', 'Jedlo.ID = Obsahuje.Nazov_jedla')->get();
		
		$res = $query->result_array();
		

		
		
		$output = "<?xml version=\"1.0\" ?>\n"; 
		$output .= "<jedalnicek>"; 

		foreach($res as $row) { 
		   $output .= "<jedlo>";
		   foreach($row as $key => $value) {
			   $output .= "<$key>";
			   $output .= $value;
			   $output .= "</$key>";
		   }

		   $output .= "</jedlo>"; 
		} 

		$output .= "</jedalnicek>"; 

		
		
		$data['title'] = 'NIS - Export';
		
		if (!write_file('./downloads/export.xml', $output))
		{
			$data['error'] = 'Nastala chyba pri vytváraní jedálnička.';
		}
		else
		{
			$this->load->view('templates/header', $data);
			$this->load->view('project/export_view', $data);
			$this->load->view('templates/footer');
		}
		
	}
}